<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-11-17
 * Time: 15:49
 */

namespace app\src\message\sms;


use app\src\base\exception\BusinessException;
use app\src\message\facade\MessageEntity;
use app\src\message\interfaces\IMessage;

class QCloudSms implements IMessage
{
    var $url;
    var $nationUrl;
    var $sdk_app_id;
    var $app_key;
    var $tplId;

    var $nationCode;
    var $phoneNumber;
    var $content;


    function __construct($config)
    {

        // url 需要根据我们说明文档上适时调整
        $this->url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
        $this->nationUrl = "https://yun.tim.qq.com/v5/tlssmssvr/sendisms";

        if (!isset($config['sdk_app_id']) || !isset($config['app_key'])) {
            throw new BusinessException('sms error params');
        }

        $this->sdk_app_id = $config['sdk_app_id'];
        $this->app_key = $config['app_key'];
    }

    public function init(MessageEntity $msg)
    {
        $this->nationCode = trim($msg->getCountry(), "+");
        $this->phoneNumber = $msg->getMobile();
        $this->content = $msg->getContent();

        return $this;
    }

    /**
     * @return mixed
     */
    function create()
    {

    }

    /**
     * @return mixed
     */
    function send($tplId = false, $params = [])
    {
        $randNum = rand(100000, 999999);
        $wholeUrl = $this->getApiUrl() . "?sdkappid=" . $this->sdk_app_id . "&random=" . $randNum;
        $tel = $this->getTel();
        $curTime = time();
        $jsonData = new \stdClass();
        if ($tplId) {
            $jsonData->tpl_id = $tplId;
            $jsonData->params = $params;
        } else {
            $jsonData->msg = $this->content;
        }
        $jsonData->time = $curTime;
        $jsonData->tel = $tel;
        $jsonData->type = "0";
        $jsonData->sig = $this->calculateSig($this->app_key, $randNum, $curTime, [$this->phoneNumber], $tel);

        $jsonData->extend = "";     // 根据需要添加，一般保持默认
        $jsonData->ext = "";        // 根据需要添加，一般保持默认
        $curlPost = json_encode($jsonData);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $wholeUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec($ch);
        $info = "";
        $result = ['status' => false, 'info' => ''];
        if ($ret === false) {
            $info = (curl_error($ch));
        } else {

            $json = json_decode($ret, JSON_OBJECT_AS_ARRAY);
            if ($json['result'] != 0) {
                $info = $this->getErrMsg($json['result']);
            } else {
                $result['status'] = true;
                $info = lang('tip_sms_success');
            }
        }

        curl_close($ch);

        $result['info'] = $info;
        return $result;
    }

    /**
     * @return string
     */
    public function getApiUrl()
    {
        if ($this->nationCode == '86') {
            return $this->url;
        }
        return $this->nationUrl;
    }

    public function getTel()
    {
        if ($this->nationCode == '86') {
            $tel = new \stdClass();
            $tel->nationcode = $this->nationCode;
            $tel->mobile = $this->phoneNumber;
        } else {
            $tel = '+' . $this->nationCode . $this->phoneNumber;
        }
        return $tel;
    }

    private function calculateSig($appkey, $random, $curTime, $phoneNumbers, $tel = '')
    {
        if ($this->nationCode == '86') {
            $phoneNumbersString = $phoneNumbers[0];
            for ($i = 1; $i < count($phoneNumbers); $i++) {
                $phoneNumbersString .= ("," . $phoneNumbers[$i]);
            }
            return hash("sha256", "appkey=" . $appkey . "&random=" . $random
                . "&time=" . $curTime . "&mobile=" . $phoneNumbersString);
        } else {
            return hash("sha256", "appkey=" . $appkey . "&random=" . $random
                . "&time=" . $curTime . "&tel=" . $tel);
        }

    }

    private function getErrMsg($code)
    {
        $err = [];
        $err['1001'] = lang('err_qcloud_1001');
        $err['1002'] = lang('err_qcloud_1002');
        $err['1003'] = lang('err_qcloud_1003');
        $err['1004'] = lang('err_qcloud_1004');
        $err['1006'] = lang('err_qcloud_1006');
        $err['1007'] = lang('err_qcloud_1007');
        $err['1008'] = lang('err_qcloud_1008');
        $err['1009'] = lang('err_qcloud_1009');
        $err['1011'] = lang('err_qcloud_1011');
        $err['1012'] = lang('err_qcloud_1012');
        $err['1013'] = lang('err_qcloud_1013');
        $err['1014'] = lang('err_qcloud_1014');
        $err['1015'] = lang('err_qcloud_1015');
        $err['1016'] = lang('err_qcloud_1016');
        $err['1017'] = lang('err_qcloud_1017');
        $err['1018'] = lang('err_qcloud_1018');
        $err['1019'] = lang('err_qcloud_1019');
        $err['1020'] = lang('err_qcloud_1020');

        $start = 1021;
        for (; $start < 1027; $start++) {
            $err[strval($start)] = lang('err_qcloud_' . $start);
        }
        $start = 1030;
        for (; $start < 1034; $start++) {
            $err[strval($start)] = lang('err_qcloud_' . $start);
        }

        if (array_key_exists(''.$code, $err)) {
            return $err[$code];
        }

        return lang('err_qcloud_unknown');
    }


}